package com.hcb.tonguetiphealth.manager;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;

import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.model.User;

public class MemberManager {
	private String tableName;
	private String TABLE_Member;
	private DBManager db;
	private ChatManager chatManager;
	private List<User> mMemberList;
	public MemberManager(Activity ctx) {
		db = DBManager.getInstance(ctx);
		chatManager = new ChatManager(ctx);
		
		String id = Global.user==null?"":Global.user.getUserId().trim();
		tableName = "member_" + id;
		TABLE_Member = "create table " + tableName + " (_id integer primary key autoincrement, "
				+ "userid text not null, username text not null, truename text not null, headicon text not null);";
		if(!db.tableIsExist(tableName)) {
			db.creatTable(TABLE_Member);
		}
		mMemberList = new ArrayList<User>();
	}
	/**
	 *  保存好友列表
	 * @param memberList
	 * @return
	 */
	public boolean saveMember(List<User> memberList) {
		if(memberList == null) {
			return false;
		} 
		//db.deleteAll(tableName);
		getmMemberList();
		boolean find = false;
		for(User member : memberList) {
			find = false;
			for(User mMember : mMemberList) {
				if(member.getUserName().equals(mMember.getUserName())) {
					find = true;
					updateMember(member);
					break;
				}
			}
			if(!find) {
				insertMember(member);
			}
			
		}
		
		return true;
	}
	/**
	 * 保存好友
	 * @param member
	 * @return
	 */
	public boolean saveMember(User member) {
		if(member == null) {
			return false;
		} 
		getmMemberList();
		boolean find = false;
		for(User mMember : mMemberList) {
			if(member.getUserName().equals(mMember.getUserName())) {
				find = true;
				updateMember(member);
				break;
			}
		}
		if(!find) {
			insertMember(member);
		}
		
		return true;
	}
	
	/**
	 * 获取好友列表
	 * @return
	 */
	private List<User> getmMemberList() {
		User user = null;
		Cursor mCursor = db.getCursor(tableName);
		if (mCursor != null) {
			mMemberList.clear();
			while(mCursor.moveToNext()) {
				user = new User();
				user.setId(mCursor.getInt(0));
				user.setUserId(mCursor.getString(1));
				user.setUserName(mCursor.getString(2));
				user.setRealName(mCursor.getString(3));
				user.setImgUrl(mCursor.getString(4));

				user.addChat(chatManager.getChatList(user, false));
				mMemberList.add(user);
			}
			mCursor.close();
		}
		
		return mMemberList;
	}
	/**
	 * 获取好友列表
	 * @return
	 */
	public List<User> getMemberList(List<User> memberList) {
		memberList.clear();
		memberList.addAll(getmMemberList());
		
		return memberList;
	}
	/**
	 * 更新好友信息
	 * @param member
	 */
	private void updateMember(User member) {
		ContentValues values = new ContentValues();
		values.put("userid", member.getUserId());
		values.put("username", member.getUserName());
		values.put("truename", member.getRealName());
		values.put("headicon", member.getImgUrl());
		db.update(tableName, "username", member.getUserName(), values);
	}
	/**
	 * 插入好友信息
	 * @param member
	 */
	private void insertMember(User member) {
		ContentValues values = new ContentValues();
		values.put("userid", member.getUserId());
		values.put("username", member.getUserName());
		values.put("truename", member.getRealName());
		values.put("headicon", member.getImgUrl());
		db.insert(tableName, values);
	}
}
